// 倒计时  getTime1：数据日期 getTime2当前日期
import dayjs from "dayjs";

export const countdown = (end) => {
  var beginDate = new Date();
  var diff = dayjs(end).valueOf() - beginDate.getTime();
  //这里计算如果是负数的处理
  var sec = diff < 0 ? Math.abs(diff / 1000) * -1 : diff / 1000;
  return formatTime(sec);
};

function formatTime(times) {
  let time = Math.abs(times);
  const diffDays = Math.floor(time / 86400);
  const diffHours = Math.floor((time % 86400) / 3600);
  const diffMinutes = Math.floor((time % 3600) / 60);
  const diffSeconds = Math.floor(time % 60);
  const days = diffDays > 0 ? `${diffDays}` : '';
  const hours = diffHours < 10 ? '0' + diffHours : diffHours;
  const minutes = diffMinutes < 10 ? '0' + diffMinutes : diffMinutes;
  const seconds = diffSeconds < 10 ? '0' + diffSeconds : diffSeconds;
  let curStatus = times < 0 ? '已结束' : ''

  return {
    curStatus,
    days,
    hours,
    minutes,
    seconds,
  }
}
